<?php
namespace app\admin\controller;

use \think\Controller as Controller;
use \think\Config;
use \app\admin\model\Site as SiteModel;
use \app\admin\model\General as GeneralModel;
use \think\Image as Image;

class  System extends Base {

    public function site(){
        $model = new SiteModel();
        $options = $model->field([
            'site_id',
            'site_name',
            'site_keyword',
            'site_disc',
            'site_email',
            'site_ipc',
            'site_phone',
            'site_address',
            'site_logo',
        ])->find();
        $this->assign('options',$options);
        return $this->fetch();
    }


    public function setsite(){
        $options = $this->req->post();
        $datas = [
            'site_id' => $options['site_id'],
            'site_name'=>$options['site_name'],
            'site_keyword'=>$options['site_keyword'],
            'site_disc'=>$options['site_disc'],
            'site_email'=>$options['site_email'],
            'site_ipc'=>$options['site_ipc'],
            'site_phone'=>$options['site_phone'],
            'site_address'=>$options['site_address'],
        ];

        $image = $this->req->file('site_logo');
        if($image){
            $info = $image->move(ROOT_PATH . 'public' . DS . 'Uploads/image');
            $logo = '/public'.DS. 'Uploads/image'.DS.$info->getSaveName();
        }
        $datas['site_logo'] = $logo;
        
        $model = new SiteModel;
        $result = $model->update($datas);
        if($result){
            $this->success("添加成功");
        }else{
            $this->error('添加失败');
        }
    }

    public function uplogo(){
        $image = $this->req->file('site_logo');
        if($image){
            $info = $image->move(ROOT_PATH . 'public' . DS . 'Uploads/image');
            $logo = '/public'.DS. 'Uploads/image'.DS.$info->getSaveName();
        }
        echo $logo;
        // echo "/public/Uploads/image/20180509/f29dbbf5a6a4203ef461ca6f4b7c354e.jpg";
    }

    public function general(){
        $model = new GeneralModel;
        $data = $model->where(['g_id'=>1])->find();
        $options = unserialize($data['g_options']);
        $this->assign('options',$options);
        $this->assign('id',$data['g_id']);
        return $this->fetch();
    }

    public function addgeneral(){
        $_data = $this->req->param();
        $gid = $_data['g-id'];
        $_data['image'] = null;
        unset($_data['g-id']);
        $water = $_data['water'];
        $image = $this->req->file('image');

        
        if($water == ''){
            if($image){
                $info = $image->move(ROOT_PATH.'public/Uploads'.DS.'site');
                $_data['image'] = '/Uploads'.DS.'site'.DS.$info->getSavename();
            }
        }else{
            if($image){
                $info = $image->move(ROOT_PATH.'public/Uploads'.DS.'site');
                $_data['image'] = '/Uploads'.DS.'site'.DS.$info->getSavename();
            }else{
                $_data['image'] = $_data['water'];
                
            }
        }
        unset($_data['water']);
        $options = serialize($_data);
        $datas['g_options'] = $options;
        $model = new GeneralModel;
        $res = $model->where(['g_id'=>$gid])->update($datas);
        if($res){
            $this->success('更新成功');
        }else{
            $this->error('更新失败');
        }
    }

    public function setimage(){
        
        $image = Image::open('Uploads/22.jpg');
        // $crop_image = $image->crop(500, 500,1100,350)->save('Uploads/33.png');
        // $image->thumb(500,500)->save('Uploads/32.jpg');
        // $image->flip()->save('Uploads/35.jpg');
        // $image->water('Uploads/cat/logo.png',3,80)->save('Uploads/36.jpg');
        $image->text('小强是蟑螂的名字','SIMLI.TTF',60,'#3F51B5',5,20,20)->save('Uploads/37.jpg');
        var_dump($image);
        // var_dump($crop_image);
    }

    public function showimage()
    {
        
    }


    public function move(){
        return $this->fetch();
    }

}
